#!/bin/bash

# Copyright (c) 2022 FuTeng
# FuYao-RepoPackageTest-System is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

# #################################################
# @Author    :   futeng
# @Contact   :   lanshaa123@163.com
# @Desc      :   创建docker镜像脚本
# @Note      :   宿主机需要安装docker
# #################################################

source ./parameters/global-variable.sh

# set -ex
# docker image id:version
tag=repotest-$I_UUID:repo
echo $I_UUID

umount -f /mnt 1> /dev/null 2>/dev/null

mount -o loop $1 /mnt

# isoName=$1

# 配置被测isoyum源
mkdir -p /root/old-repos
mv /etc/yum.repos.d/*.repo /root/old-repos
if [ -d /mnt/BaseOS ]
then
	echo "[iso]
name = iso
baseurl = file:///mnt/BaseOS/
gpgcheck = 0
enabled =1" > /etc/yum.repos.d/test.repo
else
	echo "[iso]
name = iso
baseurl = file:///mnt/
gpgcheck = 0
enabled =1" > /etc/yum.repos.d/test.repo
fi
yum makecache

echo "sslverify=False" >> /etc/yum.conf

yum_config=/etc/yum.conf
target=$(pwd)/baseimage
echo ""
echo $target
echo ""

# option defaults
if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
	yum_config=/etc/dnf/dnf.conf
	alias yum=dnf
fi

rm -rf $target
mkdir -p $target
mkdir -m 755 "$target"/dev
mknod -m 600 "$target"/dev/console c 5 1
mknod -m 600 "$target"/dev/initctl p
mknod -m 666 "$target"/dev/full c 1 7
mknod -m 666 "$target"/dev/null c 1 3
mknod -m 666 "$target"/dev/ptmx c 5 2
mknod -m 666 "$target"/dev/random c 1 8
mknod -m 666 "$target"/dev/tty c 5 0
mknod -m 666 "$target"/dev/tty0 c 4 0
mknod -m 666 "$target"/dev/urandom c 1 9
mknod -m 666 "$target"/dev/zero c 1 5

# amazon linux yum will fail without vars set
if [ -d /etc/yum/vars ]; then
	mkdir -p -m 755 "$target"/etc/yum
	cp -a /etc/yum/vars "$target"/etc/yum/
fi

install_groups=("Minimal Install")
install_packages=('bash' 'yum' 'tar' 'gzip' '*-gpg-keys' 'coreutils')


#yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
# 		--setopt=group_package_types=mandatory -y groupinstall "${install_groups[@]}"
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
 		--setopt=group_package_types=mandatory -y install "${install_packages[@]}"
# yum -c "$yum_config" --installroot="$target" -y clean all
# yum -c "$yum_config" install  --installroot="$target"  -y $(pwd)/repo/*

yum -c "$yum_config" --installroot="$target" -y clean all

# 配置
cat > "$target"/etc/sysconfig/network << EOF
NETWORKING=yes
HOSTNAME=repotest.repotest
EOF

# set DNF infra variable to container for compatibility
echo 'container' > /etc/dnf/vars/infra

# install only en_US.UTF-8 locale files
echo '%_install_langs en_US.UTF-8' > /etc/rpm/macros.image-language-conf

# force each container to have a unique machine-id
> /etc/machine-id

# 删除不必要的文件
#effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
#  docs and man pages
rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
#  cracklib
rm -rf "$target"/usr/share/cracklib
#  i18n
rm -rf "$target"/usr/share/i18n
#  yum cache
rm -rf "$target"/var/cache/yum
mkdir -p --mode=0755 "$target"/var/cache/yum
#  sln
rm -rf "$target"/sbin/sln
#  ldconfig
rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
mkdir -p --mode=0755 "$target"/var/cache/ldconfig
# remove unnecessary files
rm -f "$target"/var/lib/dnf/history.* "$target"/run/nologin
rm -fr "$target"/var/log/* "$target"/tmp/* "$target"/tmp/.* || true
# cp bash
cp /etc/skel/.bash* "$target"/root/
echo "" > "$target"/root/.bash_history
mkdir -p "$target"/etc/yum.repos.d/
mkdir -p "$target"/repotest
cat $REPO_FILE_DIR/test-repo.repo > "$target"/etc/yum.repos.d/test.repo
echo "sslverify=False" >> "$target"/etc/yum.conf

# save version
tar -C $target -c . |docker import - $tag

# clean workspace
rm -rf "$target"

# 复位宿主机yum源
rm -rf /etc/yum.repos.d/test.repo
mv /root/old-repos/*.repo /etc/yum.repos.d/
umount /mnt
rm -rf /root/old-repos
sed -i '/sslverify=False/d' /etc/yum.conf
